﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;
using SpreadsheetGear;
using SpreadsheetGear.Windows.Forms;
using System.Windows.Forms;
using DesignerHelper.EventHandlers;
using DesignerHelper.Help;
using DesignerHelper.Main;

namespace DesignerHelper.Commands
{
    public class FileCommand
    {
        public FileCommand(IBookForm form)
        {
            this.form = form;
            ActiveView = form.WorkbookView;
        }

        private WorkbookView ActiveView { get; }
        private IBookForm form;

        #region File相关方法


        /// <summary>
        /// 将当前控件窗口中打开的工作簿文件另存为
        /// </summary>
        public bool FileSaveAs()
        {
            IWorkbook book = ActiveView.ActiveWorkbook;
            using (SaveFileDialog savefile = GetSaveFileDialog(book.Name))
            {
                switch (savefile.ShowDialog())
                {
                    case DialogResult.OK:
                        ActiveView.DoWork(() =>
                        {
                            // ReSharper disable once AccessToDisposedClosure
                            book.SaveAs(savefile.FileName, GetFileformat(savefile.FilterIndex));//ps check null?
                        });
                        //OnBookIsModified(new BookSetEventArgs(book, BookSetEventArgs.Type.Refresh));
                        form.GetForm().Text = savefile.FileName;
                        MesBox.InfoMessage("成功保存文件 " + '\n' + savefile.FileName);
                        return true;
                    case DialogResult.Cancel:
                        return false;
                    default:
                        return false;
                }
            }
        }

        /// <summary>
        /// 打印预览
        /// </summary>
        /// <param name="printWhat">打印选项,指定打印的内容</param>
        public void Preview(SpreadsheetGear.Printing.PrintWhat printWhat)
        {
            ActiveView.DoWork(() =>
            {
                ActiveView.PrintPreview(printWhat);
            });
        }

        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="printWhat">打印选项,指定打印的内容</param>
        public void Print(SpreadsheetGear.Printing.PrintWhat printWhat)
        {
            ActiveView.DoWork(() =>
            {
                ActiveView.Print(printWhat, true);
            });
        }

        /// <summary>
        /// 保存当前控件窗口中打开的工作簿文件
        /// </summary>
        public bool SaveFile()
        {
            IWorkbook workbook = ActiveView.ActiveWorkbook;
            if (workbook.FullName == workbook.Name && workbook.Name.StartsWith("Book")) //ps 可能需要改进文件保存逻辑
            {
                FileSaveAs();
                return true;
            }
            else
            {
                try
                {
                    ActiveView.DoWork(() =>
                    {
                        workbook.Save();
                        //OnBookIsModified(new BookSetEventArgs(workbook, BookSetEventArgs.Type.Refresh));
                    });
                    return true;
                }
                catch (IOException ioEx)
                {
                    MesBox.ErrorMessage(ioEx);
                    return false;
                }
            }
        }

        private static SaveFileDialog GetSaveFileDialog(string fileName)
        {
            SaveFileDialog savefile = new SaveFileDialog
            {
                Filter = Properties.Resources.FileDialogFormatFilter,
                FilterIndex = 2,
                OverwritePrompt = true,
                FileName = fileName,
                CheckPathExists = true,
                ValidateNames = true,
                AutoUpgradeEnabled = true,
            };
            return savefile;
        }


        private FileFormat GetFileformat(int filterIndex)
        {
            switch (filterIndex)
            {
                case 2:
                    return FileFormat.OpenXMLWorkbook;

                case 3:
                    return FileFormat.Excel8;

                case 4:
                    return FileFormat.CSV;

                default:
                    return ActiveView.ActiveWorkbook.FileFormat;
            }
        }

        #endregion File相关方法

    }
}
